import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dialogs/myDialog.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
          useMaterial3: false,
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Flutter App"),
          ),
          body: const MyHome(),
        ));
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  void _alertDialog() async {
    //showDialog返回的对象是Future,需要使用async await方法获取结果
    var result = await showDialog(
        //点击外层遮罩是否关闭弹窗,默认为true
        barrierDismissible: false,
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("提示信息!"),
            content: const Text("确定要删除吗?"),
            actions: [
              TextButton(
                  onPressed: () {
                    print("确定");
                    //让alertDialog消失,同时可以传递dialog参数到外层
                    Navigator.pop(context, "确定");
                  },
                  child: const Text("确定")),
              TextButton(
                  onPressed: () {
                    print("取消");
                    //让alertDialog消失,同时可以传递dialog参数到外层
                    Navigator.pop(context, "取消");
                  },
                  child: const Text("取消")),
            ],
          );
        });
    print("alertDialog返回的结果:$result");
  }

  void _simpleDialog() async{
    var result = await showDialog(
        //点击外层遮罩是否关闭弹窗,默认为true
        barrierDismissible: false,
        context: context,
        builder: (context){
          return SimpleDialog(
              title: const Text("请选择语言!"),
              children: [
                SimpleDialogOption(
                  child: const Text("中文"),
                  onPressed: (){
                    print("中文");
                    Navigator.pop(context,"中文");
                  },
                ),
                SimpleDialogOption(
                  child: const Text("英语"),
                  onPressed: (){
                    print("英语");
                    Navigator.pop(context,"英语");
                  },
                ),
                SimpleDialogOption(
                  child: const Text("日语"),
                  onPressed: (){
                    print("日语");
                    Navigator.pop(context,"日语");
                  },
                ),
              ],
          );
        }
    );
    print("simpleDialog返回的结果:$result");
  }

  void _modalBottomSheet() async{
    var result =  await showModalBottomSheet(
        //点击外层遮罩是否关闭弹窗,默认为true
        isDismissible:false,
        context: context,
        builder: (context){
          return SizedBox(
            height: 240,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ListTile(
                  title: const Text("分享"),
                  onTap: (){
                    print("分享");
                    Navigator.pop(context,"分享");
                  },
                ),
                const Divider(),
                ListTile(
                  title: const Text("收藏"),
                  onTap: (){
                    print("收藏");
                    Navigator.pop(context,"收藏");
                  },
                ),
                const Divider(),
                ListTile(
                  title: const Text("取消"),
                  onTap: (){
                    print("取消");
                    Navigator.pop(context,"取消");
                  },
                ),
              ],
            ),
          );
        }
    );
    print("modalBottomSheet返回的结果:$result");
  }

  void _toast(){
    Fluttertoast.showToast(
        msg: "This is Toast",
        toastLength: Toast.LENGTH_LONG,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }

  void _myDialog(){
    showDialog(
        barrierDismissible: false,
        context: context,
        builder: (context){
          return const MyDialog(title: "信息",content: "这是一个自定义Dialog");
        }
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(onPressed: _alertDialog, child: const Text("AlertDialog_弹出框")),
          ElevatedButton(onPressed: _simpleDialog, child: const Text("SimpleDialog_选择框")),
          ElevatedButton(onPressed: _modalBottomSheet, child: const Text("ModalBottomSheet_底部弹出框")),
          ElevatedButton(onPressed: _toast, child: const Text("toast_提示框")),
          ElevatedButton(onPressed: _myDialog, child: const Text("自定义Dialog")),
        ],
      ),
    );
  }
}
